﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace IT_Pro.Forms
{
    public partial class frmCVXuLyYeuCauSuaChua : DevExpress.XtraEditors.XtraForm
    {
       // public string _tthaitruoc;
        public frmCVXuLyYeuCauSuaChua()
        {
            InitializeComponent();
        }

        public frmCVXuLyYeuCauSuaChua(string ycso,string tenloi,string loailoi,string nguyennhan,string ghichu,string khacphuc)
        {
            InitializeComponent();
            lblSo.Text = ycso;
            //_tthaitruoc = tt;
            lblLoi.Text = tenloi;
            loadDL(loailoi);
            if (nguyennhan.Length > 2)
            {
                if (nguyennhan.IndexOf("-") > 0)
                {
                    string[] strtxt = nguyennhan.Split('-');
                    cboDSNguyenNhan.EditValue = strtxt[0].Trim();
                    for(int i=1;i<strtxt.Length;i++)
                    {
                        txtLoiKhac.Text += strtxt[i];
                    }
                }
                else
                {
                    cboDSNguyenNhan.EditValue = nguyennhan;
                }
                
                txtKhacPhuc.Text = khacphuc;
                txtGhiChu.Text = ghichu;

            }
            
        }

        private void frmCVXuLyYeuCauSuaChua_Load(object sender, EventArgs e)
        {
            dateNgayXL.DateTime = DateTime.Now;
        }

        void loadDL(string _loailoi)
        {

            DataTable dtnv = Class.NV_NhanVien.HienThiDsNhanVien();
            dtnv.Rows.InsertAt(dtnv.NewRow(), 0);
            dtnv.Rows[0]["MaNV"] = "";
            dtnv.Rows[0][1] = "[Chọn Nhân Viên]";

            cboNguoiTH.Properties.DataSource = dtnv;
            cboNguoiTH.Properties.DisplayMember = "TenNV";
            cboNguoiTH.Properties.ValueMember = "MaNV";
            cboNguoiTH.EditValue = Class.DataServices.Manv;

            DataTable dtsl = Class.CV_GhiNhanYeuCau.LoadDanhSachSuLy();
            cboTrangThai.Properties.DataSource = dtsl;
            cboTrangThai.Properties.DisplayMember = "TenSuLy";
            cboTrangThai.Properties.ValueMember = "IDSL";
           
                cboTrangThai.EditValue = dtsl.Rows[3][0].ToString();        
         

            Class.CV_GhiNhanYCSuaChua gn = new Class.CV_GhiNhanYCSuaChua();
            gn.Loi =int.Parse( _loailoi);
            DataTable dtloi = gn.LoadDsLoi();
            cboDSNguyenNhan.Properties.DataSource = dtloi;
            cboDSNguyenNhan.Properties.DisplayMember = "TenLoaiLoi";
            cboDSNguyenNhan.Properties.ValueMember= "TenLoaiLoi";
            cboDSNguyenNhan.EditValue=dtloi.Rows[0][1].ToString();
        }

        private void btnThucHien_Click(object sender, EventArgs e)
        {
            if (txtKhacPhuc.Text.Length < 1)
            {
                MessageBox.Show("Chưa nhập hướng khắc phục");
                return;
            }
            if (cboNguoiTH.EditValue.ToString().Length < 2)
            {
                MessageBox.Show("Chưa Chọn nhân viện thực hiện");
                return;

            }

            Class.CV_GhiNhanYCSuaChua th = new Class.CV_GhiNhanYCSuaChua();
            th.Id =int.Parse( lblSo.Text);


            if (cboTrangThai.EditValue.ToString() != "3")
            {
                th.NgayHT = dateNgayXL.DateTime;
            }else
            {
                th.NgayHT = DateTime.Parse("1-1-2001");
            }
           
            th.NguoiTH = cboNguoiTH.EditValue.ToString();
            if (txtLoiKhac.Visible == false)
            {
                th.NguyenNhan = cboDSNguyenNhan.Text;
            }
            else
            {
                th.NguyenNhan = cboDSNguyenNhan.Text+" - " +txtLoiKhac.Text;
            }
            th.KhacPhuc = txtKhacPhuc.Text;
            th.GhiChu = txtGhiChu.Text;
            th.XuLy =int.Parse( cboTrangThai.EditValue.ToString());

            try
            {
                th.CapNhatXuLyYC();
                MessageBox.Show("Thao tác thành công !");
                this.Close();
            }
            catch
            {
                MessageBox.Show("Lỗi - Thao tác Thất bại!");

            }


        }

        private void cboDSNguyenNhan_EditValueChanged(object sender, EventArgs e)
        {
            if (cboDSNguyenNhan.Text.IndexOf("Khác") > 0)
            {
                txtLoiKhac.Visible = true;
            }
            else
            {
                txtLoiKhac.Visible = false;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}